package api

import (
	"buding-ai/internal/service"
	"buding-ai/internal/web/dto"
	"buding-ai/internal/web/response"
	"buding-ai/pkg/utils/paginate"
	"github.com/gin-gonic/gin"
	"strconv"
)

// UserApi 用户API结构体
type UserApi struct {
	router  *gin.RouterGroup
	userSrv *service.UserService
	BaseApi
}

// NewUserApi 创建用户API实例
func NewUserApi(router *gin.RouterGroup) *UserApi {
	api := &UserApi{
		router:  router,
		userSrv: service.NewUserService(),
	}
	api.router.POST("/add", api.add)
	api.router.DELETE("/delete", api.delete)
	api.router.POST("/update", api.update)
	api.router.GET("/getById", api.getById)
	api.router.GET("/page", api.page)
	api.router.GET("/restPassword", api.restPassword)
	return api
}

// add 添加用户
func (api *UserApi) add(ctx *gin.Context) {
	var userDTO dto.UserCreateDTO
	if err := ctx.ShouldBindJSON(&userDTO); err != nil {
		response.ParamBindError(ctx, err)
		return
	}

	user, err := api.userSrv.Create(userDTO)
	if err != nil {
		switch err {
		case service.ErrUserExists:
			response.ParamError(ctx, "用户名已存在")
		case service.ErrPhoneExists:
			response.ParamError(ctx, "手机号已存在")
		case service.ErrEmailExists:
			response.ParamError(ctx, "邮箱已存在")
		default:
			response.InternalError(ctx, "创建用户失败", err)
		}
		return
	}

	response.Success(ctx, "添加用户成功", user)
}

// delete 删除用户
func (api *UserApi) delete(ctx *gin.Context) {
	idStr := ctx.Query("id")
	id, err := strconv.ParseUint(idStr, 10, 64)
	if err != nil {
		response.ParamError(ctx, "无效的用户ID")
		return
	}

	err = api.userSrv.Delete(id)
	if err != nil {
		switch err {
		case service.ErrUserNotFound:
			response.NotFound(ctx, "用户不存在")
		default:
			response.InternalError(ctx, "删除用户失败", err)
		}
		return
	}

	response.Success(ctx, "删除用户成功", nil)
}

// update 更新用户信息
func (api *UserApi) update(ctx *gin.Context) {
	var userDTO dto.UserUpdateDTO
	if err := ctx.ShouldBindJSON(&userDTO); err != nil {
		response.ParamBindError(ctx, err)
		return
	}

	err := api.userSrv.Update(userDTO)
	if err != nil {
		switch err {
		case service.ErrUserNotFound:
			response.NotFound(ctx, "用户不存在")
		case service.ErrPhoneExists:
			response.ParamError(ctx, "手机号已被其他用户使用")
		case service.ErrEmailExists:
			response.ParamError(ctx, "邮箱已被其他用户使用")
		default:
			response.InternalError(ctx, "更新用户信息失败", err)
		}
		return
	}

	response.Success(ctx, "更新用户信息成功", nil)
}

// getById 根据ID获取用户信息
func (api *UserApi) getById(ctx *gin.Context) {
	idStr := ctx.Query("id")
	id, err := strconv.ParseUint(idStr, 10, 64)
	if err != nil {
		response.ParamError(ctx, "无效的用户ID")
		return
	}

	user, err := api.userSrv.GetById(id)
	if err != nil {
		switch err {
		case service.ErrUserNotFound:
			response.NotFound(ctx, "用户不存在")
		default:
			response.InternalError(ctx, "获取用户信息失败", err)
		}
		return
	}

	response.Success(ctx, "获取用户信息成功", user)
}

// page 分页获取用户列表
func (api *UserApi) page(ctx *gin.Context) {
	// 获取分页参数
	pageRequest := paginate.GetPageParams(ctx)

	pageResult, err := api.userSrv.GetPage(pageRequest)
	if err != nil {
		response.InternalError(ctx, "获取用户列表失败", err)
		return
	}

	response.SuccessWithData(ctx, pageResult)
}

// restPassword 重置用户密码
func (api *UserApi) restPassword(ctx *gin.Context) {
	idStr := ctx.Query("id")
	id, err := strconv.ParseUint(idStr, 10, 64)
	if err != nil {
		response.ParamError(ctx, "无效的用户ID")
		return
	}

	err = api.userSrv.ResetPassword(id)
	if err != nil {
		switch err {
		case service.ErrUserNotFound:
			response.NotFound(ctx, "用户不存在")
		default:
			response.InternalError(ctx, "重置密码失败", err)
		}
		return
	}

	response.Success(ctx, "密码已重置为123456", nil)
}
